package com.mutual.system.user.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutual.commom.constant.GeneralConstant;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.system.role.pojo.Role;
import com.mutual.system.role.service.RoleService;
import com.mutual.system.user.pojo.User;
import com.mutual.system.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

	@Resource
	private UserService userService;
	
	@Resource
	private RoleService roleService;
	
	@RequestMapping(value = "/toList")
	public String list(){
		return "system/user/userList";
	}
	
	@RequestMapping(value = "/listData")
	@ResponseBody
	public ResultList<User> listData(@RequestParam Map<String,Object> param){
		total=userService.selectUserListCount(param);
		ResultList<User> result=new ResultList<>();
		result.setCount(total);
		List<User> userList=userService.selectUserList(param);
		result.setData(userList);
		return result;
	}
	
	@PostMapping(value="/login")
	public String login(User user,HttpServletRequest request,Model model){
		User loginUser=userService.userLogin(user);
		if(loginUser==null||loginUser.getState()==User.ISLOCK_YES){
			model.addAttribute("loginFailed", loginUser==null?"用户名或密码错误":"您的账户已被锁定，请联系业务人员");
			return "login/login";
		}
		request.getSession().setAttribute(GeneralConstant.CURRENT_USER, loginUser);
		return "redirect:index";
			
	}
	
	@RequestMapping(value="/index")
	public String index(){
		return "common/index";
	}
	
	@RequestMapping(value="/admin")
	public String admin(){
		return "common/admin";
	}
	
	@RequestMapping(value = "/toAddUser")
	public String toAddUser(Model model){
		List<Role> roleList=roleService.findAll();
		model.addAttribute("roleList", roleList);
//		total=userService.selectUserListCount(param);
//		param.put("pageSize", pageSize);
//		List<User> userList=userService.selectUserList(param);
//		super.setDataList(userList, param, total);
		return "system/user/addUser";
	}
	
	@RequestMapping(value = "/toUpdateUser")
	public String toUpdateUser(Model model,int id){
		List<Role> roleList=roleService.findAll();
		User user=userService.selectByPrimaryKey(id);
		model.addAttribute("roleList", roleList);
		model.addAttribute("user", user);
		return "system/user/updateUser";
	}
	
	@RequestMapping(value = "/addUser")
	@ResponseBody
	public boolean addUser(User user){
		boolean flag=false;
		try {
			Date date=new Date();
			user.setCreateDate(date);
			user.setUpdateDate(date);
			userService.insert(user);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping(value = "/updateUser")
	@ResponseBody
	public boolean updateUser(User user){
		boolean flag=false;
		try {
			Date date=new Date();
			user.setUpdateDate(date);
			userService.updateByPrimaryKey(user);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	
	@RequestMapping(value = "/updateUserPassword")
	@ResponseBody
	public boolean updateUserPassword(User user){
		boolean flag=false;
		try {
			user.setId(getCurrentUser().getId());
			Date date=new Date();
			user.setUpdateDate(date);
			user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
			userService.updateUserPassword(user);
			getCurrentUser().setPassword(user.getPassword());
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	
	@RequestMapping(value = "/resetPassword")
	@ResponseBody
	public boolean updateUserPassword(@RequestParam Map<String,Object> param){
		boolean flag=false;
		try {
			param.put("ids", param.get("ids").toString().split(","));
			param.put("password", DigestUtils.md5DigestAsHex("111111".getBytes()));
			userService.resetUsersPassword(param);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@RequestMapping(value = "toUpdateUserPassword")
	public String toUpdateUserPassword(){
		return "system/user/upateUserPassword";
	}
	
	@RequestMapping(value = "exit")
	public String exit(HttpServletRequest request){
		request.getSession().removeAttribute(GeneralConstant.CURRENT_USER);
		return "login/login";
	}
}
